import wx
import wx.html
import os.path
from robot.parsing import TestDataDirectory, TestCaseFile, TestCase

class RobotBrowserCtrl(wx.html.HtmlWindow):
    def __init__(self, parent, id, *args, **kwargs):
        super(RobotBrowserCtrl, self).__init__(parent, id, *args, **kwargs)
        
    def Copy(self):
        '''Copy selected text to clipboard
        
        TODO: consider doing a "smart copy" where we put
        tsv data on the clipboard
        '''
        return self.SelectionToText()

    def CanCopy(self):
        '''Return True if there is selected text that can be copied'''
        if len(self.SelectionToText()) > 0:
            return True
        return False

    def Load(self, robot_object):
        if isinstance(robot_object, TestCase):
            self._load_testcase(robot_object)
        elif isinstance(robot_object, TestCaseFile):
            self._load_file_suite(robot_object)
        elif isinstance(robot_object, TestDataDirectory):
            self._load_directory_suite(robot_object)
        else:
            print "I don't know what to do with", robot_object


    def _get_longname(self, obj):
        longname = []
        while obj is not None:
            # The darn robot object model doesn't have a simple
            # parent/child relationship between suites and test cases.
            # The parent of a test case is a test case table rather
            # than a test suite. Go figure. So, only keep track of the
            # names of suites and test cases and not the intermediate
            # objects
            if (isinstance(obj, TestDataDirectory) or 
                isinstance(obj, TestCaseFile) or 
                isinstance(obj, TestCase)):
                longname.insert(0, obj.name)
            obj = obj.parent
        return longname

    def _get_longname_markup(self, obj):
        longname = self._get_longname(obj)
        longname[-1] = "<font size=+3, color=black><b>%s</b></font>" % longname[-1]
        name = "<b>.</b>".join(longname)
        name = "<font size=+1, color=gray>%s</font>" % name
        return name

    def _load_file_suite(self, suite):
        html = self._get_longname_markup(suite) + "<br>"
        html += "<table>\n"
        html += "</table>\n"
        self.SetPage(html)

    def _load_directory_suite(self, suite):
        html = self._get_longname_markup(suite) + "<br>"
        html += "<table>\n"
        html += "</table>\n"
        self.SetPage(html)

    def _load_testcase(self, tcuk):
        html = self._get_longname_markup(tcuk) + "<br>"
        html += "<table width=100%>\n"
#        html += "<tr><td colspan=2>%s</td></tr>\n" % self._get_longname_markup(tcuk)
        html += "<tr><td width=100 align=right valign=top>Description:</td>"
        html += "<td bgcolor=lightgray>%s</td></tr>\n" % tcuk.doc.value.replace(r'\n', '<br>\n') 
        settings = dict([(key, item) 
                         for (key, item) 
                         in tcuk.__dict__.items() 
                         if tcuk.is_setting(key)])
        for key in sorted(settings.keys()):
            item = settings[key]
            if item.is_set():
                value = "<font color=gray> | </font>" .join(item.as_list()[1:])
                html += "<tr><td align=right>%s</td><td><b>%s</b></td></tr>\n" % (key, value)
#            else:
#                value = "<font color=lightgray><i>&lt;undefined&gt;</i></font>"
        html += "</table>\n<hr>\n"
        for step in tcuk.steps:
            row = "<font color=gray> | </font>" .join(step.as_list())
            html += row + "<br>"
        self.SetPage(html)


